package com.ews.onlineexchange.controller;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;

import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

import com.ews.onlineexchange.model.Attachment;

@ManagedBean(name = "fileDownloadView2")
@SessionScoped
public class FileDownloadView2 implements Serializable {
		
		/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
		private String fileName;
		private StreamedContent file;
		
	    public StreamedContent getFile() {
	        return file;
	    }
		
		public StreamedContent download() throws UnsupportedEncodingException{
			 InputStream stream = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/resources/downloads/"+fileName);
			HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
			String contentType = req.getSession().getServletContext().getMimeType(fileName);
		     file = new DefaultStreamedContent(stream,contentType,fileName,"ISO8859-1");
		  
			return file;
	    }
		
		public StreamedContent downloadApp(String fileName) throws UnsupportedEncodingException{
			 InputStream stream = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/resources/app/"+fileName);
			HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
			String contentType = req.getSession().getServletContext().getMimeType(fileName);
		     file = new DefaultStreamedContent(stream,contentType,fileName,"ISO8859-1");
		  
			return file;
	    }
		
		public String getFileName() {
			return fileName;
		}

		public void setFileName(String fileName) {
			this.fileName = fileName;
		}   
	    private String PATH = "d://uploads//";
		public void uploadAttachment(FileUploadEvent event) {
			if (event.getFile() == null)
				return ;
			
			try {
				FileUtils.copyInputStreamToFile(event.getFile().getInputstream(), new File( PATH + event.getFile().getFileName()));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			FacesMessage message = new FacesMessage(event.getFile().getFileName() + " 上传成功。");
	        FacesContext.getCurrentInstance().addMessage(null, message);
		}
		
	    private final static String  APP_PATH = "d://hhsjzApp//";
		public void uploadAppGwt(FileUploadEvent event) {
			if (event.getFile() == null)
				return ;
			
			try {
				FileUtils.copyInputStreamToFile(event.getFile().getInputstream(), new File(APP_PATH + event.getFile().getFileName()));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			FacesMessage message = new FacesMessage(event.getFile().getFileName() + " 上传成功。");
	        FacesContext.getCurrentInstance().addMessage(null, message);
		}
		
}
